import 'dart:async';
import 'package:flutter/material.dart';
import 'package:library_base/base/base_list_state.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/event/wms_page_event.dart';
import 'package:library_base/entities/weighing_goods_child_entity.dart';
import 'package:library_base/entities/weighing_goods_entity.dart';
import 'package:library_base/event/event.dart';
import 'package:library_base/ext/list_ext.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:get/get.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/dialog/dialog_util.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';
import 'package:module_wms/page/outbound_orders/as_goods/as_goods_controller.dart';
import 'package:module_wms/page/outbound_orders/as_goods/as_goods_page_ui.dart';
import 'package:library_base/entities/event/page_refresh_event.dart';

class AsGoodsPage extends StatefulWidget {
  const AsGoodsPage({Key? key}) : super(key: key);

  @override
  _AsGoodsPageState createState() => _AsGoodsPageState();
}

class _AsGoodsPageState
    extends BaseListState<WeighingGoodsEntity, WeighingGoodsChildEntity, AsGoodsPage, AsGoodsController> {

  late StreamSubscription<WmsPageEvent> streamSubscription;

  late StreamSubscription<AsGoodsPageRefreshEvent> refreshStreamSubscription;

  @override
  void onCreate() {
    setNavigationBarVisible(false);
    setStatusBarVisible(false);
    streamSubscription = Event.eventBus.on<WmsPageEvent>().listen((event) {
      if(event.page == 1){
        if(mounted){
          var map = <String, dynamic>{};
          map['warehouse'] = baseGetxController!.warehouse;
          map['reset'] = baseGetxController!.reset;
          map['filter'] = baseGetxController!.filter;
          DialogUtil.showAsGoodsFilterDialog(arguments: map);
        }else{
          ToastUtil.normal('加载中,请稍后');
        }
      }
    });
    refreshStreamSubscription = Event.eventBus.on<AsGoodsPageRefreshEvent>().listen((event) {
      if(mounted){
        baseGetxController!.tryReLoad();
      }
    });
  }

  @override
  void controllerOnCreate() {
    super.controllerOnCreate();
    baseGetxController!.callback = refresh;
  }

  void refresh() {
    baseGetxController!.tryReLoad();
  }

  @override
  AsGoodsController registerController() => Get.put(AsGoodsController());

  @override
  bool get wantKeepAlive => true;

  @override
  Widget? getMainWidget(BuildContext context) {
    return MultiStateWidget(
      emptyWidget: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            LocalImage('common_empty', package: Constant.packLib(), width: 0.6.sw),
            Text(MultiLanguage.noSuchProduct.tr, style: TextStyles.textSecondary28),
            SizedBox(height: 80.h),
            Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              GradientButton(
                ThemeUtil.getTheme(),
                width: 0.4.sw,
                height: 80.w,
                text: MultiLanguage.backToList.tr,
                onPressed: baseGetxController!.backToList,
              ),
              SizedBox(width: 40.w),
              GradientButton(
                ThemeUtil.getTheme(),
                width: 0.4.sw,
                height: 80.w,
                text: MultiLanguage.reload.tr,
                onPressed: baseGetxController!.tryReLoad,
              ),
            ])
          ]),
      controller: baseGetxController!.multiControllerOut,
      buildChild: (){
        return Column(
          children: [
            GetBuilder<AsGoodsController>(builder: (logic) {
              return buildSearchWidget(baseGetxController!,onChanged: baseGetxController!.onChanged,onSearch: baseGetxController!.onSearch);
            }),
            Expanded(
              child: Container(
                  color: ThemeUtil.getTheme().secondaryBgColor(),
                  child: getListView(context, baseGetxController)),
            ),

            // GetBuilder<AsGoodsController>(
            //   builder: (logic) {
            //     return buildBottom(logic.stockOutChildDataEntity.value!);
            //   },
            // ),
          ],
        );
      },
      child: null,
    );
  }


  @override
  List<Widget> buildListItem(BuildContext context) {
    return baseGetxController!.listData.value.buildItem((index, element) {
      return buildItem(
        element,
        onTap: baseGetxController!.onItemChildClick,
        onItemClick: baseGetxController!.onItemClick,
      );
    }, dividingLine: () {
      return gapH18;
    });
  }

  @override
  void onDestroy() {
    super.onDestroy();
    streamSubscription.cancel();
    refreshStreamSubscription.cancel();
  }

}
